package com.genITeam.ria.dao;

import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;

import com.genITeam.ria.vo.Buddy;
import com.genITeam.ria.vo.FavouriteForum;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.ResponseVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

public class HomeDAO {

	public HomeDAO() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(HomeDAO.class);

	/**
	 * getBuddiesData Used to get the Buddies
	 * 
	 * @param memberId,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public ResponseVo getBuddiesData(int memberId, Session session)
			throws NGFException {

		ResponseVo responseVo = new ResponseVo();
		List buddyList = null;
		try {
			logger.info("Start getBuddiesData");
			buddyList = session.createCriteria(Buddy.class).add(
					Expression.eq("memberId", new Integer(memberId))).list();

			ArrayList buddy = new ArrayList();
			buddy.addAll(buddyList);

			responseVo.setBuddy(buddy);

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}

		return responseVo;
	}

	/**
	 * getFavouriteForumData Used to get Favourite Member Data
	 * 
	 * @param memberId,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public ResponseVo getFavouriteForumData(int memberId, Session session)
			throws NGFException {
		ResponseVo responseVo = new ResponseVo();
		try {
			logger.info("Start getFavouriteForumData");
			List favouriteForumList = session.createCriteria(
					FavouriteForum.class).add(
					Expression.eq("memberId", new Integer(memberId))).list();

			ArrayList favouriteForumArrayList = new ArrayList();
			favouriteForumArrayList.addAll(favouriteForumList);

			responseVo.setFavouriteForum(favouriteForumArrayList);

		} catch (Exception e) {
			throw new NGFException(e.getMessage());
		}

		return responseVo;
	}

	/**
	 * getMemberThreadData Used to get Member Threads Data
	 * 
	 * @param memberId,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public ResponseVo getMemberThreadData(int memberId, Session session)
			throws NGFException {
		ResponseVo responseVo = new ResponseVo();
		Member member = null;
		try {
			logger.info("Start getMemberThreadData");
			member = (Member) session.createCriteria(Member.class).add(
					Expression.eq("id", new Integer(memberId))).uniqueResult();
			Set memberPosts = member.getPostses();

			ArrayList memberThreadArray = new ArrayList();
			memberThreadArray.addAll(memberPosts);

			responseVo.setMemberThread(memberThreadArray);

		} catch (Exception e) {
			logger.error("Error ");
			throw new NGFException(e.getMessage());
		}

		return responseVo;
	}

	public static void main(String a[]) {
		Session session = null;
		try {
			session = HibernateUtils.getSessionFactory().openSession();
			HomeDAO homeDAO = new HomeDAO();
			homeDAO.getFavouriteForumData(1, session);
			session.close();

		} catch (Exception e) {
			System.out.println(e.toString());
			session.close();
		}

	}

}
